package com.yonyou.gmmc.service.points.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.MQReceiveResult;
import org.marketing.common.dto.points.CustomerPointsDto;
import org.marketing.common.event.BizEvent;
import org.marketing.common.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ibm.icu.text.SimpleDateFormat;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.points.constant.PointsConstant;
import com.yonyou.gmmc.service.points.eventhandle.HandleResult;
import com.yonyou.gmmc.service.points.eventhandle.RegisterSigninEventHandler;
import com.yonyou.gmmc.service.points.service.PointsPayService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = PointsConstant.BASE_PATH + "/" + PointsConstant.VERSION)
@Api("积分相关API")
public class PointsController {
    private static Logger     logger = LoggerFactory.getLogger(RegisterSigninEventHandler.class);

	@Autowired
	private PointsPayService payPointsService;

	/**
	 * 接收关键事件，判断是否要发积分
	 * 
	 * @return
	 */
	@RequestMapping(value="/event",method = RequestMethod.POST)
	@ApiOperation(value="接收MQ传来的关键事件",notes="接收事件进行积分发送")
	public MQReceiveResult receiveEvent(@RequestBody BizEvent event){
		HandleResult handleResult = payPointsService.HandlePointsEvent(event);
		
		MQReceiveResult result = new MQReceiveResult();
		result.setMessage(handleResult.getMessage());
		result.setResult(handleResult.isHandle()?"200":"500");
		return result;
	}
	
    /**
     * 查询积分
     * @param userId
     * @return
     * @throws ParseException 
     */
    @RequestMapping(value = "/ttcustomerpoint",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
	public List<CustomerPointsDto>  getTtCustomerPoint(@RequestParam("flag") Long flag,HttpServletRequest request) throws ParseException{
    	String openId = request.getHeader(CommonConstant.HEADER_OPENID);
//    	String openId = "oXrcfuGP_a3daNZaMT_rbhvJ595I";
    	List<CustomerPointsDto> dolist=payPointsService.getCustomerPoints(openId,flag);
    	
    	return dolist;
	}
    
    /**
     * 查询积分明细
     * @param userId
     * @return
     */
    @RequestMapping(value = "/Countpoint",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
	public List<CustomerPointsDto>  getCountpoint(HttpServletRequest request){
	
    	String openId = request.getHeader(CommonConstant.HEADER_OPENID);
    	//String openId = "oXrcfuGP_a3daNZaMT_rbhvJ595I";
    	List<CustomerPointsDto> dolist=payPointsService.getCountpoint(openId);
    	
    	return dolist;
	}
    
    @RequestMapping(value = "/ExpirePointsTask",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "定时任务扣除到期积分", notes = "定时任务扣除到期积分")
    public Map<String,Object>  PointsExpireTask(){
        Map<String,Object> result = new HashMap<String,Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            String d = sdf.format(date);
            result = PointsExpire(d);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return result;
    }
    
    @RequestMapping(value = "/ExpirePoints",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "将已到期积分扣除", notes = "将已到期积分扣除")
    public Map<String,Object>  PointsExpire(@RequestParam("date") String date) throws ParseException{
        Map<String,Object> result = new HashMap<String,Object>();
        result = payPointsService.PointsExpire(date);
        return result;
    }
    
    @RequestMapping(value = "/conversionLog" , method = RequestMethod.GET)
    @ApiOperation(value = "查询积分兑换记录" , notes = "查询积分兑换记录")
    public Response<Map<String,Object>> conversionLog(HttpServletRequest request,@RequestParam(value = "openId",required = false,defaultValue = "") String openId_){
        Map<String, Object> result = new HashMap<String, Object>();
        String openId="";
        try{
            if(!CheckUtil.NullOrEmpty(openId_)){
                openId = openId_;
            }else{
                openId = request.getHeader(CommonConstant.HEADER_OPENID);
            }
            result = payPointsService.conversionLog(openId);
            return new Response<Map<String, Object>>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        } catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
}
    
